package nio.base;

import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class Program {
	public static void main(String args[]) throws Exception {
		RandomAccessFile raf = new RandomAccessFile("d:\\test.txt", "rw");
		FileChannel fc = raf.getChannel();

		RandomAccessFile raf1 = new RandomAccessFile("d:\\test1.txt", "rw");
		FileChannel fc1 = raf1.getChannel();

		ByteBuffer buffer = ByteBuffer.allocate(10);

		fc.read(buffer);
		fc1.read(buffer);

		buffer.rewind();

		syso(buffer);

		// buffer.flip();
		// output("调用flip()", buffer);
		//
		// syso(buffer);
		// output("调用syso()", buffer);
		//
		// buffer.flip();
		// output("调用flip()", buffer);
		//
		// syso(buffer);
		// output("调用syso()", buffer);

		// while (buffer.remaining() > 0) {
		// byte b = buffer.get();
		// // System.out.print(((char)b));
		// }

		raf.close();
		raf1.close();
	}

	private static void syso(ByteBuffer buffer) {
		while (buffer.remaining() > 0) {
			System.out.println((char) buffer.get());
		}
	}

	public static void output(String step, Buffer buffer) {
		System.out.println(step + " : ");
		System.out.print("capacity: " + buffer.capacity() + ", ");
		System.out.print("position: " + buffer.position() + ", ");
		System.out.println("limit: " + buffer.limit());
		System.out.println();
	}
}
